package girafPlace.Admin;

import girafPlace.lib.Admin;
import girafPlace.lib.Application;
import girafPlace.sql.ApplicationAdminSQL;

import java.awt.Dimension;
import java.awt.List;
import java.awt.event.ActionEvent;
import java.util.ArrayList;

import javax.swing.AbstractAction;
import javax.swing.Action;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JTextField;


public class AdminUI {
	JFrame adminui;
	private final Action action = new SwingAction();
	private JTextField textField;
	private JTextField textField_1;
	private JTextField textField_2;
	private JTextField textField_3;
	private List applist = new List();
	private final Action action_1 = new SwingAction_1();
	private ArrayList<Application> apps = new ArrayList<Application>();
	
	
	public AdminUI(Admin user) {
		adminui = new JFrame("Admin Interface");
		adminui.setSize(new Dimension(800, 800));
		adminui.getContentPane().setLayout(null);
		ApplicationAdminSQL appssql = new ApplicationAdminSQL();

		JButton btnLogUd = new JButton("Log ud");
		btnLogUd.setAction(action);
		btnLogUd.setBounds(653, 26, 117, 25);
		adminui.getContentPane().add(btnLogUd);

		JLabel lblWelcome = new JLabel("Welcome " + user.getName() + ". Have a nice stay.");
		lblWelcome.setBounds(12, 26, 337, 15);
		adminui.getContentPane().add(lblWelcome);

		try{
			apps = appssql.getAdminApplication(user);
		} catch(Exception e){
			e.printStackTrace();
		}
		

		JLabel lblYouHaveUploaded = new JLabel("You have uploaded the following applications:");
		lblYouHaveUploaded.setBounds(12, 63, 357, 15);
		adminui.getContentPane().add(lblYouHaveUploaded);
		
		textField = new JTextField();
		textField.setBounds(299, 243, 471, 196);
		adminui.getContentPane().add(textField);
		textField.setColumns(10);
		
		textField_1 = new JTextField();
		textField_1.setBounds(299, 184, 471, 19);
		adminui.getContentPane().add(textField_1);
		textField_1.setColumns(10);
		
		textField_2 = new JTextField();
		textField_2.setColumns(10);
		textField_2.setBounds(299, 481, 471, 19);
		adminui.getContentPane().add(textField_2);
		
		textField_3 = new JTextField();
		textField_3.setColumns(10);
		textField_3.setBounds(299, 544, 471, 19);
		adminui.getContentPane().add(textField_3);

		JLabel lblNewName = new JLabel("New Name:");
		lblNewName.setBounds(299, 157, 129, 15);
		adminui.getContentPane().add(lblNewName);
		
		JLabel lblNewDescription = new JLabel("New Description:");
		lblNewDescription.setBounds(299, 216, 219, 15);
		adminui.getContentPane().add(lblNewDescription);
		
		JLabel lblNewImage = new JLabel("New Image:");
		lblNewImage.setBounds(299, 454, 114, 15);
		adminui.getContentPane().add(lblNewImage);
		
		JLabel lblNewapkFile = new JLabel("New .apk file:");
		lblNewapkFile.setBounds(299, 517, 190, 15);
		adminui.getContentPane().add(lblNewapkFile);
		

		applist.setBounds(12, 111, 265, 404);
		System.out.println("listen er " + apps.size());
		for(int i=0 ; i < apps.size() ; i++)
			applist.add(apps.get(i).getName());
		adminui.getContentPane().add(applist);
		
		JButton btnLoad = new JButton("Load");
		btnLoad.setAction(action_1);
		btnLoad.setBounds(299, 111, 117, 25);
		adminui.getContentPane().add(btnLoad);
		
		adminui.setVisible(true);

	}

	private class SwingAction extends AbstractAction {
		/**
		 * 
		 */
		private static final long serialVersionUID = -1308366572602651239L;
		public SwingAction() {
			putValue(NAME, "Log Out");
			putValue(SHORT_DESCRIPTION, "Logs out.");

		}
		public void actionPerformed(ActionEvent e) {

			adminui.dispose();
		}
	}
	private class SwingAction_1 extends AbstractAction {
		/**
		 * 
		 */
		private static final long serialVersionUID = -5837388847980878285L;
		public SwingAction_1() {
			putValue(NAME, "Load");
			putValue(SHORT_DESCRIPTION, "Loads the values of the selected application.");
		}
		public void actionPerformed(ActionEvent e) {
			System.out.println(apps.get(applist.getSelectedIndex()).getName());
			
		}
	}
}
